package Client;

import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class ventanaCrear   extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnCrear;
	public static JTextField txtUser;
	public static JTextField txtPass;
	public static boolean correct = false;
	
	public ventanaCrear() {
		this.setTitle("Crear Usuarios");
		this.setLocationRelativeTo(null);
		// Cbtener panel de contenido y establecer su esquema
		Container content = getContentPane();
		content.setLayout(null);
	
		// Crear botones y cajas de texto
		txtUser=new JTextField();
		txtPass=new JTextField();
		btnCrear = new JButton(" CREAR ");
		
		btnCrear.setBounds(50, 90, 100, 20);
		txtUser.setBounds(50,40,120,20);
		txtUser.setText("Username");
		txtPass.setBounds(50,60,120,20);
		txtPass.setText("Password");
	
		content.add(btnCrear);
		content.add(txtUser);
		content.add(txtPass);
		
		// Crear una instancia de la clase interna managerButton
		// a usar para el manejo de eventos de botones
		btnCrear.addActionListener(this);
	
		//Damos medidas y visibilidad a nuestra pantalla
		setSize(300,300);
		setVisible(true);
		
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
	}
	
	public boolean isCorrect ()
	{
		return correct;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		JButton botonPulsado = (JButton)event.getSource();
		if(botonPulsado==btnCrear) {
			TCPClient.setComando("CREAR_USUARIO "+txtUser.getText()+"-"+txtPass.getText());
			TCPClient.escribirBuffer("CREAR_USUARIO "+txtUser.getText()+"-"+txtPass.getText());
			try {
				JOptionPane.showMessageDialog(null, TCPClient.leerBuffer());
			} catch (HeadlessException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.dispose();
		}
	}

}
